<?php
/*
 * A Smarty template class to work with PiePHP.
 *
 * @author     Bill Lee <bill@cachecrew.com>
 * @package    PiePHP
 * @since      Version 0.0
 * @copyright  Copyright (c) 2011, Pie Software Foundation
 * @license    http://www.piephp.com/license
 *
 * @see http://www.smarty.net/
 */
 
/**
 * If you want to base your project on Smarty-based templates, use this
 * controller for the base-class. By default, no actual actions need be
 * implemented. The default catchAllAction() will look for templates in
 * template directory organized by "controller-name"/action
 *
 * @todo Consider adding renderView() semantics.
 */
class SmartyController extends Controller {
	/**
	 * Smarty object.
	 */
	protected static $smarty;

	private static $_ext='.tpl';

	/**
	 * Common initializer called?
	 */
	private static $_init=false;
	private function _sharedInit() {
		global $SMARTY_DIR;
		
		if ($SMARTY_DIR === NULL && is_dir(dirname(dirname(__FILE__)) . '/Smarty/'))
			$SMARTY_DIR = (dirname(dirname(__FILE__)) . '/Smarty/');
		require_once $SMARTY_DIR.'Smarty.class.php';
		
		self::$_init = true;
	} // _sharedInit()
	
	public function __construct() {
		self::$_init or self::_sharedInit(); // Global init
		
		$this->smarty = new Smarty();
		if (!is_dir(Pie::getSitePath() . 'data/cache/'))
			mkdir(Pie::getSitePath() . 'data/cache/');
		$this->smarty->cache_dir = Pie::getSitePath() . 'data/cache/';
		if (!is_dir(Pie::getSitePath() . 'data/compile/'))
			mkdir(Pie::getSitePath() . 'data/compile/');
		$this->smarty->compile_dir = Pie::getSitePath() . 'data/compile/';
		$this->smarty->config_dir = Pie::getSitePath();
		if (!is_dir(Pie::getSitePath() . 'templates/'))
			mkdir(Pie::getSitePath() . 'templates/');
		$this->smarty->template_dir = Pie::getSitePath() . 'templates/';
		// $this->smarty->testInstall();
	} // __construct() 
	
	/**
	 * Set default extension for templates
	 */
	public function setExt($ext) {
		self::$_ext = $ext[0] == '.' ? $ext : ".$ext";
	}
		
	/**
	 * Infer a view based on the controller and action, and render that view.
	 * @param  $data: optional data to be passed in to the view.
	 * @param  $templateName: optional template name to override the 
	 *                        controller's default template name.
	 */
	public function render($data = array(), $templateName = NULL) {
		if (!$templateName) {
			$templateName = $this->getViewName().self::$_ext;
		}
// echo __METHOD__.'#'.__LINE__." $templateName<br/>";

		if ($data && !empty($data))
			$this->smarty->assign($data);
		$this->smarty->display($templateName);
	} // render()
	
	public static function getSmartyPath() {
		global $SMARTY_DIR;
		return $SMARTY_DIR;
	} // setPath()
	
	/**
	 * Set the path to the Smarty library (the location of the
	 * Smarty.class.php file).
	 */
	public static function setSmartyPath($path) {
		global $SMARTY_DIR;
		$SMARTY_DIR = $path;
	} // setSmartyPath()
	
	/** 
	 * Handle default dispatch to Smarty templates. If used as the baseclass
	 * for the DefaultController, then no controller implementations are needed. 
	 */
	public function catchAllAction() {
		$caller = debug_backtrace();
		$caller = $caller[1];
/*	?> <pre>Caller
	<?php 
	echo get_class($caller['object']),' ';
	echo get_class($this), ' ';
	var_dump($caller); 
	?>
	</pre><?php
*/
		$templateName = $this->getViewName().self::$_ext;
// echo __METHOD__.'#'.__LINE__." ${templateName}<br/>";

		if (isset($caller['object']) && $this == $caller['object'] 
		    && $this->smarty->templateExists($templateName)) {
			$this->render(func_get_args(), $templateName);
		}
		else {
			global $ACTION_NAME;
			$params = func_get_args();
			$ACTION_NAME = $params[0];
			array_shift($params);
			$templateName = $this->getViewName().self::$_ext;
// echo __METHOD__.'#'.__LINE__." ${templateName}<br/>";
			if ($this->smarty->templateExists($templateName)) {
				$this->render(func_get_args(), $templateName);
			}
			else
				parent::catchAllAction(); // see show404()
		}
			
	} // catchAllAction()
	
} // class SmartyController

